package de.openkanban.server.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

/**
 * entity class for user
 */
@Entity
public class User implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long userId;

	private String vorname;
	private String nachname;
	private String email;
	private String password;
	private String avatarUrl = "default.png";
	private String namensKuerzel;

	@OneToMany(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
	private List<User> kontakteEmailList = new ArrayList<User>();

	public User() {
	}

	public User(String email, String password) {
		super();
		this.email = email;
		this.password = password;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getVorname() {
		return vorname;
	}

	public void setVorname(String vorname) {
		this.vorname = vorname;
	}

	public String getNachname() {
		return nachname;
	}

	public void setNachname(String nachname) {
		this.nachname = nachname;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getAvatarUrl() {
		return avatarUrl;
	}

	public void setAvatarUrl(String avatarUrl) {
		this.avatarUrl = avatarUrl;
	}

	public String getNamensKuerzel() {
		return namensKuerzel;
	}

	public void setNamensKuerzel(String namensKuerzel) {
		this.namensKuerzel = namensKuerzel;
	}

	public List<User> getKontakteEmailList() {
		return kontakteEmailList;
	}

	public void setKontakteEmailList(List<User> kontakteEmailList) {
		this.kontakteEmailList = kontakteEmailList;
	}

	@Override
	public String toString() {
		return "User [id=" + userId + ", email=" + email + ", password=" + password + "]";
	}

	public void addContact(User contact) {
		if (kontakteEmailList == null) {
			kontakteEmailList = new ArrayList<User>();
		}

		kontakteEmailList.add(contact);
	}

	public boolean removeContact(User contact) {
		return kontakteEmailList.remove(contact);
	}

	public List<Long> getKontakteEmailListAsSharedUser() {
		List<Long> userIdList = new ArrayList<Long>();

		if (kontakteEmailList != null) {
			for (User user : kontakteEmailList) {
				userIdList.add(user.getUserId());
			}
		}
		return userIdList;
	}

}
